package cn.doitedu.rtdw.dashboard;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class JOB04_各省份参与3个活动的累计流量统计 {

    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.enableCheckpointing(5000);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");
        env.getCheckpointConfig().setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE);

        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);

        // 建表，映射 kafka中 dwd层 行为日志明细宽表
        tenv.executeSql(
                " create table dwd_events_kafka (                       "+
                        "     release_channel string                             "+
                        "     ,device_type string                                 "+
                        "     ,session_id  string                                 "+
                        "     ,lat         double                                 "+
                        "     ,lng         double                                 "+
                        "     ,username    string                                 "+
                        "     ,event_id     string                                "+
                        "     ,action_time   bigint                               "+
                        "     ,properties  map<string,string>                     "+
                        "     ,user_id  bigint                                    "+
                        " 	  ,member_level_id bigint                             "+
                        "     ,password string                                    "+
                        "     ,nickname string                                    "+
                        "     ,phone string                                       "+
                        "     ,status int                                         "+
                        "     ,create_time timestamp(3)                           "+
                        "     ,icon string                                        "+
                        "     ,gender int                                         "+
                        "     ,birthday date                                      "+
                        "     ,register_city string                               "+
                        "     ,job string                                         "+
                        "     ,personalized_signature string                      "+
                        "     ,source_type int                                    "+
                        "     ,integration int                                    "+
                        "     ,growth int                                         "+
                        "     ,lucky_count int                                    "+
                        "     ,history_integration int                            "+
                        "     ,modify_time timestamp(3)                           "+
                        "     ,province string                                    "+
                        "     ,city string                                        "+
                        "     ,region string                                      "+
                        "     ,page_type string                                   "+
                        "     ,page_service string                                "+
                        "     ,page_lanmu string                                  "+
                        "     ,page_pindao string                                 "+
                        "     ,pt AS proctime()                                   "+   // 处理时间语义
                        "     ,rt AS to_timestamp_ltz(action_time,3)              "+
                        "     ,watermark for rt as rt - interval '0' second       "+   // 事件时间语义
                        " ) with (                                                "+
                        "     'connector' = 'kafka',                              "+
                        "     'topic' = 'dwd-events',                             "+
                        "     'properties.bootstrap.servers' = 'doitedu:9092',    "+
                        "     'properties.group.id' = 'goo2',                     "+
                        "     'scan.startup.mode' = 'latest-offset',              "+
                        "     'value.format'='json',                              "+
                        "     'value.json.fail-on-missing-field'='false',         "+
                        "     'value.fields-include' = 'EXCEPT_KEY'               "+
                        " )                                                       "
        );


        // 当天累计到当前，来自于各省份的， pv量，uv量，新用户数（每1秒钟更新一次）
        tenv.executeSql(
                " WITH tmp AS (                                                                                                    "+
                        "     SELECT                                                                                                       "+
                        "         *,properties['url'] as url                                                                               "+
                        "     FROM dwd_events_kafka                                                                                        "+
                        "     WHERE  properties['url']                                                                                     "+
                        "       in ('/mall/promotion/qxlx.html',                                                                           "+
                        "       '/mall/promotion/qxcd.html',                                                                               "+
                        "       '/mall/promotion/qxjh.html') AND event_id = 'page_load'                                                    "+
                        " )                                                                                                                "+
                        "                                                                                                                  "+
                        " SELECT                                                                                                           "+
                        "     window_start,                                                                                                "+
                        " 	window_end,                                                                                                    "+
                        " 	province,                                                                                                      "+
                        " 	case                                                                                                           "+
                        " 	    when url='/mall/promotion/qxlx.html' then '七夕拉新'                                                       "+
                        " 	    when url='/mall/promotion/qxcd.html' then '七夕促单'                                                       "+
                        " 	    when url='/mall/promotion/qxjh.html' then '七夕激活'                                                       "+
                        "     end as promotion_name,	                                                                                   "+
                        " 	count(1) as pv,                                                                                                "+
                        " 	count(distinct user_id) as uv,                                                                                 "+
                        " 	count(distinct user_id) FILTER(WHERE TO_DATE(date_format(create_time,'yyyy-MM-dd')) = CURRENT_DATE) as new_uv  "+
                        " FROM TABLE(                                                                                                      "+
                        "     CUMULATE(TABLE tmp,DESCRIPTOR(rt),INTERVAL '1' SECOND,INTERVAL '24' HOUR)                                    "+
                        " )                                                                                                                "+
                        " GROUP BY                                                                                                         "+
                        "     window_start,                                                                                                "+
                        " 	window_end,                                                                                                    "+
                        " 	province,                                                                                                      "+
                        " 	url                                                                                                            "
        ).print();
    }
}
